using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace generics { class Program { class Node where T : System.IComparable { public T element; public Node left = null; public Node right = null; Node parent = null; public Node createNode(T element) { Node n = new Node() { parent = this, element = element }; return n; } public Node addLeft(T element) { this.left = createNode(element); return this.left; } public Node addRight(T element) { this.right = createNode(element); return this.right; } public override string ToString() { return "Node element => " + element.ToString(); } } static Node dfs(T val, Node parent) where T : System.IComparable { if (parent == null) return null; if (parent.element.CompareTo(val) == 0) return parent; Node ret = null; if (parent.left != null) ret = dfs(val, parent.left); if (ret != null) return ret; if (parent.right != null) ret = dfs(val, parent.right); if (ret != null) return ret; return null; } static void Main(string[] args) { // Binary tree but with any type for the object Node n = new Node(); n.addLeft(4).addLeft(10).addLeft(7); n.addRight(5).addRight(2); Console.WriteLine(dfs(2, n)); } } }